﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
using System.Xml.Xsl;

namespace PeshiSoft.DbmlUpdater
{
	class Program
	{
		private static string sqlMetalPath;
		static void Main(string[] args)
		{
			Console.WriteLine();
			Console.WriteLine(" DBML Updater v2.0");
			Console.WriteLine(" ------------------------------------------------------------------------------ ");

			if (!CheckWinSDK())
				return;

			Worker worker = new Worker(sqlMetalPath);

			var files = Directory.GetFiles(Environment.CurrentDirectory, "*.config", SearchOption.AllDirectories);
			foreach (string f in files)
			{
				if (f.StartsWith(Path.Combine(Environment.CurrentDirectory, "obj\\"), StringComparison.CurrentCultureIgnoreCase))
					continue;

				worker.Generate(f);				
			}
			Console.WriteLine();
			Console.WriteLine(" ------------------------------------------------------------------------------ ");
			Console.WriteLine("                                                           " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));

#if DEBUG
			Console.ReadLine();
#endif
		}

		private static bool CheckWinSDK()
		{
			List<string> paths = new List<string>() 
			{ 
				"v7.0\\Bin\\SqlMetal.exe",
				"v7.0\\Bin\\x64\\SqlMetal.exe",
				"v6.0A\\Bin\\SqlMetal.exe",
				"v6.0A\\Bin\\x64\\SqlMetal.exe"
			};

			string programFiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Microsoft SDKs\\Windows\\");

			for (int i = 0; i < paths.Count; i++)
			{
				sqlMetalPath = Path.Combine(programFiles, paths[i]);
				if (File.Exists(sqlMetalPath))
					return true;
			}

			Console.WriteLine("ERROR: SqlMetal.exe not found. Install Windows SDK.");
			return false;
		}
	}
}
